# 数据文件迷你化
import os
import pandas as pd


class CsvMiniUtil:
    def __init__(self):
        print("-----CsvMiniUtil初始化")

    @staticmethod
    def miniCsv(folder_path_tmp):
        # 1-不带后缀的
        folder_path_no_prefix = folder_path_tmp.split('/')[0] if folder_path_tmp.endswith('/') else folder_path_tmp
        folder_path = folder_path_no_prefix + "/"
        folder_path_mini = folder_path_no_prefix + "_mini/"
        files = [f for f in os.listdir(folder_path) if f.endswith('.csv')]
        for file in files:
            file_path = os.path.join(folder_path, file)
            # 使用pandas读取文件的前1000行
            data = pd.read_csv(file_path, nrows=1000)

            # 将新的文件名设为原文件名加上'_new'后缀
            new_file = file.split('.')[0] + '.csv'
            new_file_path = os.path.join(folder_path_mini, new_file)

            # 将数据写入新的文件
            data.to_csv(new_file_path, index=False)

            # 打印文件名称
            print(file)
